/**
 * 
 */
package GUI.main.renderers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import GUI.helpers.ProjectConstants;

import officeserver.users.PersonalInfo;

/**
 * @author mramsey3
 * 
 */
public class PatientListRenderer extends JPanel implements ListCellRenderer {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel logoPanel;
    private JPanel textPanel;
    private JLabel nameLabel;

    /**
     * 
     */
    public PatientListRenderer() {
	setLayout(new BorderLayout());
	setBorder(new LineBorder(ProjectConstants.BORDER_SUB_COLOR,2));
	add(getLogoPanel(), BorderLayout.WEST);
	add(getTextPanel(), BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getLogoPanel() {
	if (logoPanel == null) {
	    logoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.BORDER_SUB_COLOR);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
		    g.drawString("P", 14, getHeight() - 12);
		}
	    };
	    logoPanel.setPreferredSize(new Dimension(50, 50));
	    logoPanel.setBorder(BorderFactory.createLineBorder(ProjectConstants.BORDER_SUB_COLOR, 8));
	    logoPanel.setOpaque(false);

	}
	return logoPanel;
    }
    
    /**
     * @return
     */
    private JPanel getTextPanel(){
	if(textPanel == null){
	    textPanel = new JPanel();
	    textPanel.setLayout(new BorderLayout());
	    textPanel.setBorder(new MatteBorder(2,0,2,2,ProjectConstants.BORDER_SUB_COLOR));
	    textPanel.add(getNameLabel(),BorderLayout.CENTER);
	    textPanel.setOpaque(false);
	}
	return textPanel;
    }

    /**
     * @return
     */
    private JLabel getNameLabel(){
	if(nameLabel == null){
	    nameLabel = new JLabel();
	    nameLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
	    nameLabel.setOpaque(false);
	}
	return nameLabel;
    }
    
    /**
     * @param text
     */
    private void setNameLabelText(String text){
	getNameLabel().setText(text);
    }
    
    public Component getListCellRendererComponent(JList list, Object value,
	    int arg2, boolean isSelected, boolean arg4) {
	PersonalInfo pInfo = (PersonalInfo) value;
	setNameLabelText("  "+pInfo.getFirstName() + " " + pInfo.getLastName());
	if (isSelected) {
	    setBackground(list.getSelectionBackground());
	    setForeground(list.getSelectionForeground());
	    setBorder(new LineBorder(ProjectConstants.BORDER_MAIN_COLOR,2));
	} else {
	    setBackground(list.getBackground());
	    setForeground(list.getForeground());
	    setBorder(new LineBorder(Color.white,2));
	}
	setEnabled(list.isEnabled());
	setFont(list.getFont());
	setOpaque(true);
	setPreferredSize(new Dimension(getWidth(), 52));

	return this;
    }

}
